using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace AvalonEditDemo.Services
{
    /// <summary>
    /// 将布尔值转换为可见性的值转换器
    /// true -> Visible, false -> Collapsed
    /// </summary>
    public class BooleanToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// 将布尔值转换为可见性
        /// </summary>
        /// <param name="value">要转换的布尔值</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">转换参数，未使用</param>
        /// <param name="culture">区域性信息</param>
        /// <returns>如果值为true，则返回Visibility.Visible；否则返回Visibility.Collapsed</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value is bool boolValue && boolValue) 
                ? Visibility.Visible 
                : Visibility.Collapsed;
        }

        /// <summary>
        /// 将可见性转换回布尔值
        /// </summary>
        /// <param name="value">要转换的可见性值</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">转换参数，未使用</param>
        /// <param name="culture">区域性信息</param>
        /// <returns>如果值为Visibility.Visible，则返回true；否则返回false</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is Visibility visibility && visibility == Visibility.Visible;
        }
    }

    /// <summary>
    /// 反转布尔值的转换器
    /// 可以选择性地将反转后的布尔值转换为可见性
    /// </summary>
    public class InverseBooleanConverter : IValueConverter
    {
        /// <summary>
        /// 将布尔值反转，或者将布尔值反转后转换为可见性
        /// </summary>
        /// <param name="value">要转换的布尔值</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">如果为"Visibility"，则将结果转换为可见性</param>
        /// <param name="culture">区域性信息</param>
        /// <returns>反转后的布尔值，或相应的可见性值</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 处理特殊情况：转换为Visibility
            if (parameter != null && parameter.ToString().Equals("Visibility", StringComparison.OrdinalIgnoreCase))
            {
                if (value is bool boolValue)
                {
                    return boolValue ? Visibility.Collapsed : Visibility.Visible;
                }
                return Visibility.Visible;
            }
            
            // 标准布尔值反转
            if (value is bool standardBoolValue)
            {
                return !standardBoolValue;
            }
            
            return value;
        }

        /// <summary>
        /// 将转换后的值转换回原始值
        /// </summary>
        /// <param name="value">要转换回去的值</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">如果为"Visibility"，则从可见性转换回布尔值</param>
        /// <param name="culture">区域性信息</param>
        /// <returns>反转后的原始值</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 处理特殊情况：从Visibility转换回布尔值
            if (parameter != null && parameter.ToString().Equals("Visibility", StringComparison.OrdinalIgnoreCase))
            {
                if (value is Visibility visibility)
                {
                    return visibility != Visibility.Visible;
                }
                return false;
            }
            
            // 标准布尔值反转
            if (value is bool standardBoolValue)
            {
                return !standardBoolValue;
            }
            
            return value;
        }
    }

    /// <summary>
    /// 将字符串是否为空转换为布尔值的转换器
    /// 空字符串 -> false, 非空字符串 -> true
    /// </summary>
    public class StringEmptyToBooleanConverter : IValueConverter
    {
        /// <summary>
        /// 将字符串是否为空转换为布尔值
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">转换参数，未使用</param>
        /// <param name="culture">区域性信息</param>
        /// <returns>如果字符串为空或null，则返回false；否则返回true</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !(value is string stringValue && string.IsNullOrEmpty(stringValue));
        }

        /// <summary>
        /// 不支持从布尔值转换回字符串
        /// </summary>
        /// <exception cref="NotImplementedException">此方法始终抛出异常</exception>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
} 